package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xxxx.server.mapper.PositionMapper;
import com.xxxx.server.pojo.Position;
import com.xxxx.server.service.IPositionService;
import com.xxxx.server.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {


    @Resource
    private PositionMapper positionMapper;

    /**
     * 添加职位信息
     * @param entity
     * @return
     */
    @Override
    public boolean save(Position entity) {
        //职位姓名不能为空，否则抛出异常
        AssertUtil.isTrue(null == entity.getName(),"职位名称不能为空！");
        //职位唯一，否则抛出异常
        AssertUtil.isTrue(null != positionMapper.selectByName(entity.getName()),"职位名称已存在！");
        //设置默认值
        entity.setCreateDate(LocalDateTime.now());
        entity.setEnabled(true);
        //执行添加操作
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    public boolean updateById(Position entity) {
        //职位姓名不能为空，否则抛出异常
        AssertUtil.isTrue(null == entity.getName(),"职位名称不能为空！");
        //根据姓名获取对象
        Position temp = positionMapper.selectByName(entity.getName());
        //职位唯一且id一致，否则抛出异常
        AssertUtil.isTrue(null != temp && !temp.getId().equals(entity.getId()),"职位名称已存在！");
        //设置默认值
        entity.setCreateDate(LocalDateTime.now());
        //执行修改操作
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    public boolean deletePosition(Integer id) {
        //id非空判断
        AssertUtil.isTrue(null == id,"系统异常，请重试！");
        //根据id查询的对象非空判断
        if (null != positionMapper.selectById(id)) {
            AssertUtil.isTrue(positionMapper.deletePosition(id) < 1,"职位删除失败！");
            return true;
        }
        return false;
    }
}
